<template>
  <div>

    <!-- 定义面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 定义卡片位置 -->
    <el-card class="box-card">
      <!-- 定义表格数据 -->
      <el-table :data="rightsList" style="width: 100%" border stripe row-key="id">
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="权限名称" prop="name"></el-table-column>
        <el-table-column label="路径" prop="path"></el-table-column>
        <el-table-column label="权限等级" prop="level">
          <!-- 通过作用域插槽的方式定义格式 -->
          <template slot-scope='scope'>
            <el-tag effect='dark' v-if="scope.row.level=== 1">一级</el-tag>
            <el-tag effect='dark' type="success" v-if="scope.row.level=== 2">二级</el-tag>
            <el-tag effect='dark' type="warning" v-if="scope.row.level=== 3">三级</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
              <!-- 定义2个按钮-->
              <el-button type="primary" icon="el-icon-edit"  @click="updateRightsBtn(scope.row)"  size="mini"></el-button>
              <el-button type="danger" icon="el-icon-delete"   size="mini"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

<!-- 添加权限修改对话框 -->
    <el-dialog title="修改权限信息" :visible.sync="updateRightsDialogVisible" width="50%">

      <el-form :model="updateRightsForm" :rules="rules" ref="upDateRightsForm" label-width="100px">
        <el-form-item label="权限名称:" prop="name">
          <el-input v-model="updateRightsForm.name"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="updateRightsDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateRights">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
  export default {
    created() {
      //定义初始化获取权限列表的方法
      this.getRightsList()
    },
    data() {
      return {
        //定义权限列表数据
        rightsList: [],
        updateRightsDialogVisible: false,
        updateRightsForm: {}
      }
    },
    methods: {
      async getRightsList() {
        const {
          data: result
        } = await this.$http.get('/rights/getRightsListAll')
        if (result.status !== 200) return this.$message.error('权限列表获取失败')
        this.rightsList = result.data
        console.log(this.rightsList)
      },
      async updateRights() {
        //修改权限信息
        const {
          data: result
        } = await this.$http.put('/rights/updateRights', this.updateRightsForm)
        if (result.status !== 200) return this.$message.error("更新权限信息失败")
        this.$message.success("更新权限信息成功")
        this.getRightsList()
        this.updateRightsDialogVisible = false;
      },
      updateRightsBtn(rights)
      {
          this.updateRightsForm=rights
          this.updateRightsDialogVisible = true
      }
    }
  }
</script>

<style>
</style>
